package com.briup.jz.web.controller;

import com.briup.jz.bean.Article;
import com.briup.jz.bean.Category;
import com.briup.jz.bean.extend.ArticleExtend;
import com.briup.jz.service.IArticleService;
import com.briup.jz.service.ICategoryService;
import com.briup.jz.utils.CustomerException;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Api(description="资讯相关接口")
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @GetMapping("/read")
    @ApiOperation(value = "查询资讯详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "资讯id",paramType = "query",dataType ="String",required = true)
    })
    public Message read(long id){
        ArticleExtend articleExtend = articleService.read(id);
        return MessageUtil.success(articleExtend);
    }
    @ApiOperation(value = "查询资讯")
    @ApiImplicitParams({
            @ApiImplicitParam(name="status",value = "咨询审核状态",paramType = "query",dataType ="String"),
            @ApiImplicitParam(name="title",value = "咨询标题",paramType = "query",dataType ="String"),
            @ApiImplicitParam(name="categoryId",value = "咨询所属分类的Id",paramType = "query",dataType ="long"),
    })
    @GetMapping("/query")
    public Message query(Article article){
        List<Article> list = articleService.query(article);
        return MessageUtil.success(list);
    }

    @GetMapping("/queryCascade")
    @ApiImplicitParams({
            @ApiImplicitParam(name="status",value = "咨询审核状态",paramType = "query",dataType ="String"),
            @ApiImplicitParam(name="title",value = "咨询标题",paramType = "query",dataType ="String"),
            @ApiImplicitParam(name="categoryId",value = "咨询所属分类的Id",paramType = "query",dataType ="long"),
    })
    public Message queryCascade(String status, String title, Long categoryId){
        List<ArticleExtend> list = articleService.queryCascade(status,title,categoryId);
        return MessageUtil.success(list);
    }



    @ApiOperation(value = "通过id删除资讯")
    @GetMapping("/deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
    public Message deleteById(@NotNull Long id){
        articleService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation(value = "保存或更新咨询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", paramType = "form"),
            @ApiImplicitParam(name = "title", value = "资讯标题", paramType = "form"),
            @ApiImplicitParam(name = "content", value = "资讯内容", paramType = "form"),
            @ApiImplicitParam(name = "categoryId", value = "资讯所属分类的Id", paramType = "form"),
            @ApiImplicitParam(name = "cover", value = "资讯封面的url", paramType = "form"),
    })
    @PostMapping("/saveOrUpdate")
    public Message saveOrUpdate(Article article) {
        articleService.saveOrUpdate(article);
        return MessageUtil.success("更新成功");
    }
}
